package kr.hkit.framework.dao;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class LoadPropertyDAO {
	private static final Properties PROPERTIES = new Properties();
	private static final String PROPERTIES_FILE = "dao.properties";
	
	private static String DRIVER;
	private static String URL;
	private static String USER;
	private static String PASSWORD;
	
	private static LoadPropertyDAO instance = new LoadPropertyDAO();
	
	public LoadPropertyDAO() {
		try {
			InputStream is = new BufferedInputStream(new FileInputStream(PROPERTIES_FILE));
			PROPERTIES.load(is);
			
			DRIVER = PROPERTIES.getProperty("DRIVER");
			URL = PROPERTIES.getProperty("URL")+PROPERTIES.getProperty("DATABASE");
			USER = PROPERTIES.getProperty("USER");
			PASSWORD = PROPERTIES.getProperty("PASSWORD");
			
			System.out.println(DRIVER + " : " + URL + " : " + USER + " : " + PASSWORD);
			
			Class.forName(DRIVER);	// Class.forname() DRIVER클래스를 로딩하겠다
											//여기서 DRIVER는 mysql라이브러리에 있는 연동관련 클래스.
			
		} catch (FileNotFoundException e) {
				throw new DAOConfigurationException(PROPERTIES_FILE+"is not found", e);
		} catch (IOException e) {
			throw new DAOConfigurationException("connot load to properties file", e);
		} catch (ClassNotFoundException e) {
			throw new DAOConfigurationException("Driver class '" + DRIVER + "' is missing",e);
		}
	
	}
	
	private Connection creatConnection() throws DAOConfigurationException{
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			throw new DAOConfigurationException("ERROR : driver is not fount", e);
		}
		return connection;
	}

	public static Connection getconConnection() throws DAOConfigurationException {
		return instance.creatConnection();
	}
}
